package com.chem.lems.serves.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.chem.lems.constans.ResultCodeConstant;
import com.chem.lems.entity.Equipment;
import com.chem.lems.entity.User;
import com.chem.lems.repository.EquipmentRepository;
import com.chem.lems.repository.UserRepository;
import com.chem.lems.serves.ExcelServices;
import com.chem.lems.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ExcelServicesImpl implements ExcelServices {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private EquipmentRepository equipmentRepository;

    public ResultUtil userExcelExport() {
        List<User> userList = userRepository.findAll();
        List<Equipment> equipmentList = equipmentRepository.findAll();
        log.info(userList.toString());
        ExcelWriterBuilder writerBuilder = EasyExcel.write("Equipment.xlsx", Equipment.class);
        ExcelWriterSheetBuilder sheet = writerBuilder.sheet();
        sheet.doWrite(equipmentList);
        log.info("写入完毕");
        return new ResultUtil(ResultCodeConstant.SUCCESS, "获取设备状态列表成功！");
    }
}
